package com.example.shop_2212.cateDetail

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.shop_2212.R
import com.example.shop_2212.adapter.GoodsAdapter
import com.example.shop_2212.databinding.ActivityCateDetailBinding
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode

@Route(path = "/app/CateDetailActivity")
class CateDetailActivity : BaseActivity<ActivityCateDetailBinding,CateDetailViewModel>() {
    lateinit var goodsAdapter: GoodsAdapter
    @Autowired
    @JvmField
    var cid = 515
    @Autowired
    @JvmField
    var cname = "全部"

    var page = 1
    var limit = 10
    var keyword = ""
    var news = false
    var salesOrder = ""
    var priceOrder = ""

    override fun initView() {
        ARouter.getInstance().inject(this)
        binding.tvCname.text = cname
        goodsAdapter  = GoodsAdapter()
        binding.rv.adapter = goodsAdapter
        binding.rv.layoutManager = GridLayoutManager(this,2)
        //请求数据
        viewModel.cateDetail(cid,keyword, limit, news, page, priceOrder, salesOrder)

        //点击销量排序
        binding.clSales.setOnClickListener {
            if(salesOrder==""){//当前排序为无序，点击后变成升序
                salesOrder = "asc"
                binding.ivSales.setImageResource(R.drawable.sort3)
            }else if(salesOrder=="asc"){
                salesOrder = "desc"
                binding.ivSales.setImageResource(R.drawable.sort2)
            }else{
                salesOrder = ""
                binding.ivSales.setImageResource(R.drawable.sort1)
            }
            //价格是无序的
            priceOrder = ""
            binding.ivPrice.setImageResource(R.drawable.sort1)
            viewModel.cateDetail(cid, keyword, limit, news, page, priceOrder, salesOrder)
        }
        //点击价格排序
        binding.clPrice.setOnClickListener {
            if(priceOrder==""){//当前排序为无序，点击后变成升序
                priceOrder = "asc"
                binding.ivPrice.setImageResource(R.drawable.sort3)
            }else if(priceOrder=="asc"){
                priceOrder = "desc"
                binding.ivPrice.setImageResource(R.drawable.sort2)
            }else{
                priceOrder = ""
                binding.ivPrice.setImageResource(R.drawable.sort1)
            }
            //销量是无序的
            salesOrder = ""
            binding.ivSales.setImageResource(R.drawable.sort1)
            viewModel.cateDetail(cid, keyword, limit, news, page, priceOrder, salesOrder)
        }
        //点击新品
        binding.tvIsNew.setOnClickListener {
            if(news){//当前为红色，点击后，变为黑色
                binding.tvIsNew.setTextColor(Color.BLACK)
                news = false
            }else{
                binding.tvIsNew.setTextColor(Color.RED)
                news = true
            }
            viewModel.cateDetail(cid, keyword, limit, news, page, priceOrder, salesOrder)
        }
        //搜索输入内容
        binding.sv.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if(query!=null){
                    keyword  = query
                    viewModel.cateDetail(cid, keyword, limit, news, page, priceOrder, salesOrder)
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return false
            }
        })
        binding.ivBack.setOnClickListener {
            //finish()
            val addressPicker = AddressPicker(this)
            addressPicker.setTitle("配送地址")
            addressPicker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)
            addressPicker.setOnAddressPickedListener { province, city, county ->
                ToastUtils.showLong("${province.name}${city.name}${county.name}")
            }
            addressPicker.show()
        }

    }

    override fun initData() {
        viewModel.success.observe(this){
            if(it.code==Const.RESULT_OK){
                goodsAdapter.data.clear()
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<CateDetailViewModel>  = CateDetailViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_cate_detail

}